package org.csu.mybigpro.controller;


import org.csu.mybigpro.DTO.AiAssistantRequestDto;
import org.csu.mybigpro.service.AiAssistantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/ai")
@CrossOrigin // 临时允许跨域
public class AiAssistantController {

    @Autowired
    private AiAssistantService aiAssistantService;

    @PostMapping("/assistant/ask")
    // 【关键改动】接收包含完整聊天记录的DTO
    public ResponseEntity<Map<String, String>> askAssistant(@RequestBody AiAssistantRequestDto request) {
        List<AiAssistantRequestDto.ChatMessageDto> messages = request.getMessages();
        if (messages == null || messages.isEmpty()) {
            return ResponseEntity.badRequest().body(Map.of("reply", "问题不能为空"));
        }
        try {
            // 将历史记录传递给Service
            String reply = aiAssistantService.getAiAssistantReply(messages);
            return ResponseEntity.ok(Map.of("reply", reply));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.internalServerError().body(Map.of("reply", "抱歉，AI助教服务出现内部错误。"));
        }
    }
}